package com.bkd.dreams.client.stand;

import java.util.List;

import com.bkd.dreams.client.login.NotLoggedInException;
import com.bkd.dreams.client.stand.service.StandRoleService;
import com.bkd.dreams.client.stand.service.StandRoleServiceAsync;
import com.google.gwt.cell.client.ActionCell;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.TabPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

public class StandRoleTable {

	private static final Class<StandRoleService> SERVICE_CLASS = StandRoleService.class;
	private VerticalPanel standRolesPanel = new VerticalPanel();
	private CellTable<StandRole> standRolesTable = new CellTable<StandRole>();
	private HorizontalPanel editStandRolePanel = new HorizontalPanel();
	private TextBox newStandRoleNameTextBox = new TextBox();
	private Button addStandRoleButton = new Button("Add");

	private StandRoleServiceAsync roleService = (StandRoleServiceAsync)GWT.create(StandRoleService.class);

	public void loadTable(TabPanel tabPanel) {
		TextColumn<StandRole> nameColumn = new TextColumn<StandRole>() {
			@Override
			public String getValue(StandRole role) {
				return role.getName();
			}
		};
		standRolesTable.addColumn(nameColumn, "Role");
		
		ActionCell<StandRole> actionCell = new ActionCell<StandRole>(
				"Remove", new ActionCell.Delegate<StandRole>() {
					public void execute(StandRole role) {
						removeStandRole(role);
					}

				});
		Column<StandRole, StandRole> removeColumn = new Column<StandRole, StandRole>(actionCell) {
			public StandRole getValue(StandRole role) {
				return role;
			}
		};
		standRolesTable.addColumn(removeColumn, "Remove");

		editStandRolePanel.add(newStandRoleNameTextBox);
		editStandRolePanel.add(addStandRoleButton);

		standRolesPanel.add(standRolesTable);
		standRolesPanel.add(editStandRolePanel);
		
		tabPanel.add(standRolesPanel, "Roles");
		
		addStandRoleButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				addStandRole();
			}
		});
		
		refreshStandRolesTable();
	}

	private void refreshStandRolesTable() {
		if (roleService == null) {
			roleService = GWT.create(StandRoleService.class);
		}

		// Set up the callback object.
		AsyncCallback<List<StandRole>> callback = new AsyncCallback<List<StandRole>>() {
			public void onFailure(Throwable error) {
				handleError(error);
			}

			public void onSuccess(List<StandRole> result) {
				updateStandRoleTable(result);
			}
		};

		// Make the call to the stock price service.
		roleService.getAllStandRoles(callback);		
	}

	protected void updateStandRoleTable(List<StandRole> roles) {
		if (roles != null) {
			standRolesTable.setRowCount(roles.size(),true);
			standRolesTable.setRowData(0, roles);
		} else {
			standRolesTable.setRowCount(0);
		}
	}

	protected void addStandRole() {
		String name = newStandRoleNameTextBox.getText();
		if(name.equals("")) {
			return;
		}
		
		StandRole role = new StandRole(name);
		addStandRole(role);
		newStandRoleNameTextBox.setText("");
	}

	private void addStandRole(StandRole role) {
		if (roleService == null) {
			roleService = GWT.create(StandRoleService.class);
		}

		// Set up the callback object.
		AsyncCallback<Void> callback = new AsyncCallback<Void>() {
			public void onFailure(Throwable error) {
				handleError(error);
			}

			public void onSuccess(Void result) {
				refreshStandRolesTable();
			}
		};

		// Make the call to the stock price service.
		roleService.addStandRole(role, callback);
	}

	private void removeStandRole(StandRole role) {
		if (roleService == null) {
			roleService = GWT.create(StandRoleService.class);
		}

		// Set up the callback object.
		AsyncCallback<Void> callback = new AsyncCallback<Void>() {
			public void onFailure(Throwable error) {
				handleError(error);
			}

			public void onSuccess(Void result) {
				refreshStandRolesTable();
			}
		};

		// Make the call to the stock price service.
		roleService.removeStandRole(role, callback);
	}
	private void handleError(Throwable error) {
		Window.alert(error.getMessage());
		if (error instanceof NotLoggedInException) {
			//Window.Location.replace(loginInfo.getLogoutUrl());
		}
	}
}
